#include "b1.h"

D_PIXEL *linebufR[8];
D_PIXEL *linebufG[8];
D_PIXEL *linebufB[8];

int curpix;
int curlin;
int maxpix;

void b1_init(int size_x) {
  int i;
  for (i=0; i<8; ++i) {
    linebufR[i] = new D_PIXEL[size_x];
    linebufG[i] = new D_PIXEL[size_x];
    linebufB[i] = new D_PIXEL[size_x];
  }
  maxpix=size_x;
  curpix=0;
  curlin=0;
}

void b1_run(fifo<D_PIXEL> *scanR, fifo<D_PIXEL> *scanG, fifo<D_PIXEL> *scanB,
	    fifo<D_PIXEL> *b1R,   fifo<D_PIXEL> *b1G,   fifo<D_PIXEL> *b1B) {

  int i,j1,j2;

  if (! scanR->empty() ) {
    linebufR[curlin][curpix] = scanR->read();
    linebufG[curlin][curpix] = scanG->read();
    linebufB[curlin][curpix] = scanB->read();
    curpix++;
    if (curpix == maxpix) {
      curpix = 0;
      curlin++;
      if (curlin == 8) {
	curlin = 0;
	/* all blocks ready */
	for (i=0; i<(maxpix/8); ++i) { /* for all blocks      */
	  for (j2=0; j2<8; ++j2) {   /* for all pix in col  */
	    for (j1=0; j1<8; ++j1) {     /* for all pix in line */
	      b1R->write(linebufR[j2][i*8 + j1]);
	      b1G->write(linebufG[j2][i*8 + j1]);
	      b1B->write(linebufB[j2][i*8 + j1]);
	    }
	  }
	}
      }
    }
  }
}
